using NExt.Configuration.Builder;
using NExt.Configuration.Model;
using NExt.Handler.Base.Common.Manager;
using NExt.Serialization;

namespace NExt.Configuration
{
    public class NExtConfigurationLoader
    {
        public static void Load(NExtConfiguration nextConfiguration)
        {
            SetSerializer(nextConfiguration.Serializer);
            SetObjectBuilder(nextConfiguration.ObjectBuilder);
        }

        private static void SetSerializer(Serializer serializer)
        {
            if (serializer == Serializer.AjaxPro)
            {
                JsonSerializer.Load("NExt.Serialization.AjaxPro");
                return;
            }
            if (serializer == Serializer.JsonNet)
            {
                JsonSerializer.Load("NExt.Serialization.JsonNet");
                return;
            }
            
            throw new NExtConfigurationException("Serializer is not set !");
        }

        private static void SetObjectBuilder(IObjectBuilder objectBuilder)
        {
            if (objectBuilder.Builder == null)
            {
                throw new NExtConfigurationException("Object builder is not set !");
            }

            InstanceManager.SetObjectBuilder(objectBuilder.Builder);
        }
    }
}